package tech.aistar.day10.safe;

import java.beans.Visibility;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2022/8/1 11:08 上午
 */
public class ArrayListSafeDemo implements Runnable{
    //线程安全的
    private Vector<Integer> vector = new Vector<>();

    //线程不安全
    private List<Integer> list = new ArrayList<>();


    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            //vector.add(i);//add方法是一个同步方法
            list.add(i);
        }
        System.out.println(Thread.currentThread()+":"+list.size()+"=>"+list);
    }
}
class SafeTest{
    public static void main(String[] args) {
        Runnable r = new ArrayListSafeDemo();
        Thread t1 = new Thread(r);
        Thread t2 = new Thread(r);

        t1.start();
        t2.start();
    }
}
